<?php

namespace app\common\model;

use think\Model;
use think\Db;
/**
 * 礼物模型
 */
class Gift extends Model
{
    protected $append = [
        'status_text'
    ];

    public static function init(){
        self::afterInsert(function($row){
            if($row->pid){
                Db::name('gift')->where('id',$row->pid)->update(['status' => 5]);
            }
        });
    }
    public function goods(){
        return $this->belongsTo('goods','goods_id','id')->field('id,name,image,market_price,give_integral,shop_price');
    }

    public function user(){
        return $this->belongsTo('user')->field('id,nickname,avatar');
    }

    public function order(){
//        ->field('id,order_status,pay_status,type,deleted,shipping_status,item_id,order_prom_type,goods_price,goods_num');
        return $this->belongsTo('order','order_id','id');
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'),'1' => __('Status 1'),'2' => __('Status 2'),'3' => __('Status 3'),'4' => __('Status 4'),'5' => __('Status 5')];
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : $data['status'];
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }
}